package mailServlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.MailSendDAO;
import dto.UserDTO;

@SuppressWarnings("serial")
public class MailDeleteServlet extends HttpServlet {

	public MailDeleteServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}
	public void execute(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		MailSendDAO mdao=new MailSendDAO();
		UserDTO udto=(UserDTO) request.getSession().getAttribute("USER");
	    String fileName=request.getSession().getServletContext().getRealPath("\\")+"AllUsers/"+udto.getUserName();
		String []dates=request.getParameterValues("one");
		String n=request.getParameter("n");
		String sql=null;
		
		if("1".equals(n)){
			if(delete(fileName+"/receive",fileName+"/delete", dates)){
				sql="update tb_send set receive_state='3' where send_toname='"+udto.getUserName()+"' and send_date=?";
				if(mdao.update(sql, dates)){
					response.sendRedirect("MailReceiveServlet");
				}
			}
		}else{
			if(delete(fileName+"/send",fileName+"/delete", dates)){
				sql="update tb_send set send_state='3' where send_fromname='"+udto.getUserName()+"' and send_date=?";
				if(mdao.update(sql, dates)){
					response.sendRedirect("MailHaveSendServlet");
				}
			}
		}
	}

	private boolean delete(String fromFile,String toFile,String []dates){
		boolean flag=false;
		String deleName=null;
		String toName=null;
		PrintWriter pw=null;
		BufferedReader br=null;
		for(String date:dates){
			deleName=fromFile+"/"+date+".txt";
			File file1=new File(deleName);
			toName=toFile+"/"+date+".txt";
			File file2=new File(toName);
			try {
				if(!file2.exists()){
					file2.createNewFile();
				}
				br=new BufferedReader(new InputStreamReader(new FileInputStream(deleName)));
				pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(toName)));
				String str=null;
				while((str=br.readLine())!=null){
					pw.println(str);
					pw.flush();
				}
				pw.close();
				br.close();
				file1.delete();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		flag=true;
		return flag;
	}
	
	public void init() throws ServletException {
		// Put your code here
	}

}
